<script setup lang='ts'>
interface Props {
  modelValue: string
  cols?: number
  list: {
    label: string
    value: string
    count?: number
    icon?: string
    [t: string]: any
  }[]
  type: 'sports' | 'time'
}

defineOptions({ name: 'AppSportsMarketFilter' })
const props = withDefaults(defineProps<Props>(), {
  cols: 1
})
const emit = defineEmits(['update:modelValue'])

const { sportsEventTimeRang } = storeToRefs(useTransferStore())

const mainDomRef = ref()
const isLayerOpen = ref(false)

const _cols = computed(() => ` grid-cols-${props.cols}`)
// 选择球种
const isSportsType = computed(() => props.type === 'sports')
// 选择时间
const isTimeType = computed(() => props.type === 'time')
// 当前选中的球种列表
const selectedSports = computed(() => isSportsType.value ? props.modelValue.split(',') : [])
// 选中的obj
const activeItem = computed(() => {
  if (isSportsType.value) {
    return props.list.map((a) => {
      return {
        ...a,
        checked: selectedSports.value.includes(a.value)
      }
    }).filter((b) => b.checked)[0] ?? { label: '-', value: '-', count: 0, icon: '' }
  }
  else {
    return props.list.find((a) => a.value === props.modelValue) ?? { label: '-', value: '-', count: 0, icon: '' }
  }
})

function clickHandler(item: IBaseSelectItem) {
  if (isTimeType.value) {
    if (item.value === void 0 || item.value === props.modelValue)
      return

    sportsEventTimeRang.value = item.value
    emit('update:modelValue', item.value)
  }
  else if (isSportsType) {
    // 点击所有项目
    if (item.value === '0') {
      emit('update:modelValue', item.value)
    }
    else {
      // 是否已选中
      if (checkItemActive(item)) {
        const arr = selectedSports.value.filter((a) => a !== item.value)

        emit('update:modelValue', arr.length ? arr.join(',') : '0')
        return
      }
      const arr = selectedSports.value.filter((a) => a !== '0')
      arr.push(item.value)
      emit('update:modelValue', arr.join(','))
    }
  }
}
function checkItemActive(item: IBaseSelectItem) {
  return isSportsType.value ? selectedSports.value.includes(item.value) : props.modelValue === item.value
}
function toggleSelect() {
  isLayerOpen.value = !isLayerOpen.value
}
function closeLayer() {
  isLayerOpen.value = false
}

onClickOutside(mainDomRef, closeLayer)
</script>

<template>
  <div ref="mainDomRef" class="min-w-fit">
    <div class="label" @click="toggleSelect">
      <!-- icon -->
      <div v-if="activeItem.icon" class="flex-none h-[24px] w-[24px] z-[2] p-[2px] flex items-center mr-[4px]">
        <AppImage :url="activeItem.icon" is-network>
          <BaseIcon class="text-[24px]" name="uni-vector" />
        </AppImage>
      </div>
      <!-- 文字 -->
      <div class="flex-1 w-full overflow-hidden relative">
        <div class="w-full inline-block  whitespace-nowrap">
          {{ activeItem.label }}
        </div>
      </div>
      <!-- 数量 -->
      <div v-if="isSportsType && activeItem.count && activeItem.count > 0" class="count">
        <span>+{{ activeItem.count }}</span>
      </div>
      <div v-else-if="isTimeType" class="count">
        <span>{{ activeItem.count }}</span>
      </div>
      <!-- 箭头 -->
      <div class="arrow" :class="{ up: isLayerOpen }">
        <BaseIcon name="uni-triangle" />
      </div>
    </div>

    <!-- 弹出层 -->
    <div class="layer" v-if="isLayerOpen">
      <BaseSportsScrollbar class="wrap" :class="{ mask: isSportsType }">
        <div class="grid gap-[8px]" :class="_cols">
          <div v-for="item, i in list" :key="i" class="item" :class="{ active: checkItemActive(item) }"
            @click="clickHandler(item)">
            <div class="flex items-center gap-[4px]">
              <div v-if="item.icon" class="flex-none flex items-center h-[24px] w-[24px] p-[2px]">
                <AppImage :url="item.icon" is-network>
                  <BaseIcon class="text-[24px]" name="uni-vector" />
                </AppImage>
              </div>
              <div class="text-[14px] font-semibold leading-[16px] tracking-[0.28px]">
                {{ item.label }}
              </div>
              <div v-if="isTimeType" class="count">{{ item.count }}</div>
            </div>
            <div v-if="isSportsType" class="radio">
              <span class="circle" :class="{ active: checkItemActive(item) }">
                <span class="selected">
                  <div class="selected-sub1"></div>
                  <div class="selected-sub2"></div>
                </span>
              </span>
            </div>
          </div>
        </div>
      </BaseSportsScrollbar>
    </div>
  </div>
</template>

<style lang='scss' scoped>
.count {
  color: rgba(255, 255, 255, 0.7);
  padding: 2px 6px;
  font-size: 12px;
  background: rgba(255, 255, 255, 0.08);
  font-weight: 600;
  line-height: 16px;
  margin-left: 4px;
  border-radius: 12px;
  letter-spacing: 0.3px;
}

.radio {
  cursor: pointer;
  display: flex;
  font-size: 12px;
  align-items: center;
  font-weight: 600;
  line-height: 16px;
  letter-spacing: 0.03em;
  position: relative;
  margin-top: 0;

  .circle {
    width: 16px;
    height: 16px;
    z-index: 1;
    position: relative;
    margin-right: 8px;

    &::before {
      top: 0px;
      left: 0px;
      width: 16px;
      height: 16px;
      content: "";
      display: block;
      z-index: 2;
      position: absolute;
      box-sizing: border-box;
      transition: opacity 0.5s;
      border: 2px solid rgb(255, 255, 255);
      opacity: 0.5;
      background: none;
      border-radius: 50%;
      border-color: #ffffff;
    }

    &::after {
      top: 0px;
      left: 0px;
      width: 16px;
      height: 16px;
      content: "";
      display: block;
      z-index: 2;
      position: absolute;
      box-sizing: border-box;
      transition: opacity 0.5s;
      border-radius: 50%;
      opacity: 0;
      background: rgb(255, 255, 255);
    }



    &.active {
      &::after {
        opacity: 1;
        background: #000;
      }

      .selected {
        width: 22px;
        height: 22px;
        display: inline-block;
        z-index: 10;
        position: relative;
        transform: rotate(45deg);

        .selected-sub1 {
          top: 7px;
          left: 7px;
          width: 2px;
          height: 7px;
          position: absolute;
          background: #24ee89;
          border-radius: 8px;
        }

        .selected-sub2 {
          top: 13px;
          left: 4px;
          width: 5px;
          height: 2px;
          position: absolute;
          background: #24ee89;
          border-radius: 8px;
        }
      }
    }
  }
}


.label {
  cursor: pointer;
  height: 32px;
  display: flex;
  padding: 0px 12px;
  position: relative;
  font-size: 14px;
  box-sizing: border-box;
  transition: background 0.5s, opacity 0.5s, color 0.5s;
  align-items: center;
  font-weight: 600;
  line-height: 16px;
  user-select: none;
  margin-right: 8px;
  border-radius: 18px;
  color: #000000;
  background: #24ee89;

  .arrow {
    color: inherit;
    display: flex;
    transition: all 0.3s;
    align-items: center;
    justify-content: center;
    margin-left: 8px;
    margin-right: -4px;
    width: 16px;
    height: 16px;
    font-size: 16px;
    --tg-base-icon-color: #000;

    &.up {
      transform: rotate(-180deg);
    }
  }

  .count {
    color: #000000;
    background: rgba(35, 38, 38, 0.2);
  }
}

.layer {
  top: calc(100% + 8px);
  left: 0;
  width: 100%;
  z-index: 10;
  position: absolute;
  max-width: 100%;
  box-sizing: border-box;

  .wrap {
    color: rgb(255, 255, 255);
    max-width: 100%;
    box-shadow: rgba(0, 0, 0, 0.16) 0px 5px 16px;
    box-sizing: border-box;
    max-height: 380px;
    overflow: hidden auto;
    width: 100%;
    padding: 16px;
    background: #292d2e;
    border-radius: 8px;

    &.mask {
      &::after {
        left: 0px;
        width: calc(100% - 12px);
        bottom: 0px;
        height: 24px;
        content: "";
        z-index: 2;
        position: absolute;
        background: linear-gradient(to top, rgb(41, 45, 46), rgba(41, 45, 46, 0));
        pointer-events: none;
      }
    }

    .item {
      color: #ffffff;
      cursor: pointer;
      height: 40px;
      display: flex;
      padding: 0 12px;
      background: rgba(255, 255, 255, 0.1);
      min-height: 29px;
      border-radius: 32px;
      justify-content: space-between;

      &.active {
        color: #000000;
        background: #24ee89;
        --tg-base-icon-color: #000;

        .count {
          color: #000000;
          background: rgba(35, 38, 38, 0.2);
        }
      }
    }
  }
}
</style>
